package com.lark.wifi.dialog;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cdl.library.view.MyToastView;
import com.lark.wifi.base.IWifi;
import com.lark.wifi.entity.WifiEntry;
import com.mtk.launcher.R;
import com.mtk.launcher.util.shared.SharedPerManager;

/***
 * 常规需要WIFI账号密码的dialog
 */
public class MyWifiDialog extends Dialog implements View.OnClickListener {
    TextView mText, tv_safe;
    EditText wifi_pass;
    TextView btn_confirm;
    TextView btn_cancel;
    IWifi iWifi;
    WifiCallBackLink wifiCallBackLink;
    Context context;

    public MyWifiDialog(@NonNull Context context) {
        super(context, R.style.CustomDialogTheme);
        this.context = context;
    }

    public MyWifiDialog setmYtitle(IWifi iWifi) {
        this.iWifi = iWifi;
        return this;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_my_wifi);
        tv_safe = findViewById(R.id.tv_safe);
        mText = findViewById(R.id.dialog_title);
        btn_cancel = findViewById(R.id.btn_cancel);
        btn_confirm = findViewById(R.id.btn_confirm);
        wifi_pass = findViewById(R.id.wifi_pass);
        wifi_pass.setTypeface(Typeface.DEFAULT);
        mText.setText(iWifi.name());
        tv_safe.setText(iWifi.encryption());
        btn_cancel.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = SharedPerManager.getScreenWidth() / 2;
        params.height = ActionBar.LayoutParams.WRAP_CONTENT;
//        getWindow().setAttributes(params);
    }


    @Override
    public void show() {
        super.show();
        View dialogView = getWindow().getDecorView();
        dialogView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                dialogView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                wifi_pass.setFocusable(true);
                wifi_pass.requestFocus();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public MyWifiDialog setOnLinkListener(WifiCallBackLink wifiCallBackLink) {
        this.wifiCallBackLink = wifiCallBackLink;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                String inputInfoPwd = wifi_pass.getText().toString().trim();
                if (TextUtils.isEmpty(inputInfoPwd) || inputInfoPwd.length() < 8) {
                    MyToastView.getInstance().Toast(getContext(), context.getString(R.string.please_input_right_pwd));
                    return;
                }
                WifiEntry wifiEntry = new WifiEntry();
                wifiEntry.SSID = iWifi.name();
                wifiEntry.password = inputInfoPwd;
                wifiCallBackLink.onLink(wifiEntry);
                dismiss();
                break;
        }
    }

    public interface WifiCallBackLink {
        void onLink(WifiEntry wifiEntry);
    }

}
